﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Threading.Tasks;
using WebApiTest.Dto.Basic;
using WebApiTest.Dto.Utils;
using WebApiTest.Models;
using System.Collections.Generic;

namespace WebApiTest.Service.Office

{
    public class StatisticsService
    {
        officeContext db = new officeContext();

        public TableRspDTO groupByType(TableReqDTO tableReqDTO)
        {
            long count = db.Collect
                .Join(db.OfficeObject, c => c.ObjectId, o => o.ObjectId, (c, o) => new { typeId = o.TypeId })
                .GroupBy(e => e.typeId)
                .LongCount();
            List<JObject> res = db.Collect
                .Join(db.OfficeObject, c => c.ObjectId, o => o.ObjectId, 
                (c, o) => new { typeId = o.TypeId, num = c.CollectNum  })
                .GroupBy(e => e.typeId)
                .Select(e => new { key = e.Key, typeId = e.Key, frequency = e.Count(), count = e.Sum(n => n.num) })
                .OrderByDescending(u => u.typeId)
                .Skip(tableReqDTO.getStart())
                .Take(tableReqDTO.pageSize)
                .ToList()
                .ConvertAll(r => JObject.FromObject(r));
            PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.pageNo,
                tableReqDTO.pageSize,
                count,
                res
                );
            return new TableRspDTO(pagingDTO);
        }

        public TableRspDTO groupByPerson(TableReqDTO tableReqDTO)
        {
            long count = db.Collect
                .Select(e => new { personId = e.PersonId })
                .GroupBy(e => e.personId)
                .LongCount();
            List<JObject> res = db.Collect
                .Select(e => new { personId = e.PersonId, num = e.CollectNum })
                .GroupBy(e => e.personId)
                .Select(e => new { key = e.Key, personId = e.Key, frequency = e.Count(), count = e.Sum(n => n.num) })
                .OrderByDescending(u => u.personId)
                .Skip(tableReqDTO.getStart())
                .Take(tableReqDTO.pageSize)
                .ToList()
                .ConvertAll(r => JObject.FromObject(r));
            PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.pageNo,
                tableReqDTO.pageSize,
                count,
                res
                );
            return new TableRspDTO(pagingDTO);
        }

        public TableRspDTO groupByDept(TableReqDTO tableReqDTO)
        {
            long count = db.Collect
                .Join(db.Person, c => c.PersonId, p => p.PersonId, (c, p) => new {  deptId = p.DeptId })
                .GroupBy(e => e.deptId)
                .LongCount();
            List<JObject> res = db.Collect
                .Join(db.Person, c => c.PersonId, p => p.PersonId, (c, p) => new { personId = c.PersonId, deptId = p.DeptId, num = c.CollectNum })
                .GroupBy(e => e.deptId)
                .Select(e => new { key = e.Key, deptId = e.Key, frequency = e.Count(), count = e.Sum(n => n.num) })
                .OrderByDescending(u => u.deptId)
                .Skip(tableReqDTO.getStart())
                .Take(tableReqDTO.pageSize)
                .ToList()
                .ConvertAll(r => JObject.FromObject(r));
            PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.pageNo,
                tableReqDTO.pageSize,
                count,
                res
                );
            return new TableRspDTO(pagingDTO);
        }
    }
}
